package com.gitee.jjnnzb.jimmyrpc.demo.provider.controller;

import com.gitee.jjnnzb.jimmyrpc.core.entity.RpcRequest;
import com.gitee.jjnnzb.jimmyrpc.core.entity.RpcResponse;
import com.gitee.jjnnzb.jimmyrpc.core.provider.ProviderBootstrap;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jjn
 * @since 2024-03-17 22:32
 */
@RestController
@RequiredArgsConstructor
public class DemoController {
    
    private final ProviderBootstrap providerBootstrap;
    
    @RequestMapping("/")
    public RpcResponse sendRequest(@RequestBody RpcRequest request) {
        return providerBootstrap.invokeRequest(request);
    }
    
    @Bean
    public ApplicationRunner buildRunner() {
        return args -> {
            RpcRequest request = new RpcRequest()
                    .setService("com.gitee.jjnnzb.jimmyrpcapi.service.UserService")
                    .setMethod("findById").setArgs(new Object[]{100});
            RpcResponse rpcResponse = sendRequest(request);
            System.out.println(rpcResponse);
        };
    }
}
